package com.cwf.system.validation.permission;

import com.cwf.common.enums.ResCodeEnum;
import com.cwf.common.exception.base.MyException;
import com.cwf.system.entity.Permission;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.intellij.lang.annotations.Pattern;

import javax.validation.constraints.*;
import java.io.Serializable;

/**
 * 添加(Permission)实体类
 *
 * @author makejava
 * @since 2023-02-15 12:42:44
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@ToString
public class AddPermission implements Serializable {
    private static final long serialVersionUID = 210823805838544488L;
    /**
     * 功能id
     */
    @Null(groups = PermissionGroup.addMenu.class)
    @Null(groups = PermissionGroup.addMark.class)
    @Null(groups = PermissionGroup.addButton.class)
    private Long id;
    /**
     * 功能名称
     */

    @NotBlank(groups = PermissionGroup.addMenu.class)
    @NotBlank(groups = PermissionGroup.addMark.class)
    @NotBlank(groups = PermissionGroup.addButton.class)
    private String title;
    /**
     * 级别
     */
    @NotNull(groups = PermissionGroup.addMenu.class)
    @Min(groups = PermissionGroup.addMenu.class, value = 1)
    @Max(groups = PermissionGroup.addMenu.class, value = 4)
    private Integer rank;
    /**
     * 功能父级id
     */
    @NotNull(groups = PermissionGroup.addMenu.class)
    @NotNull(groups = PermissionGroup.addMark.class)
    @NotNull(groups = PermissionGroup.addButton.class)
    private Long parentId;
    /**
     * 路径
     */
    @NotBlank(groups = PermissionGroup.addMenu.class)
    @NotBlank(groups = PermissionGroup.addMark.class)
    private String path;
    /**
     * 名称
     */
    @NotBlank(groups = PermissionGroup.addMenu.class)
    private String name;
    /**
     * 组件地址
     */
    @NotBlank(groups = PermissionGroup.addMenu.class)
    private String component;
    /**
     * 重定向
     */
    private String redirect;
    /**
     * 元标签
     */
    private String meta;
    /**
     * 功能状态
     */
    private Integer state;
    /**
     * 标记
     */
    @NotBlank(groups = PermissionGroup.addButton.class)
    private String mark;
    /**
     * 分类(1是菜单2是按钮)
     */
    private Integer category;
    /**
     * 图标地址
     */
    private String icon;
    /**
     * 排序
     */
    private Integer sort;
    /**
     * 是否显示
     */
    private Integer isShow;
    /**
     * 逻辑删除
     */
    private String isDelete;


    /**
     * 添加菜单级别
     *
     * @param permission
     * @return
     */
    public static Permission addMenu(AddPermission permission) {


        if (permission.getRank() != 1 && permission.getRank() != 3) {
            throw new MyException("菜单属于1级和3级,请检查上传参数是否符合规范", ResCodeEnum.PARAM_ERR.getCode());
        }


        Permission addPermission = Permission.builder()
                .category(1)
                .rank(permission.getRank())
                .title(permission.getTitle())
                .path(permission.getPath())
                .name(permission.getName())
                .redirect(permission.getRedirect())
                .component(permission.getComponent())
                .state(permission.getState())
                .sort(permission.getSort())
                .icon(permission.getIcon()).build();


        if (permission.getRank() == 1) {
            addPermission.setParentId(0L);
        } else {
            addPermission.setRank(3);
            addPermission.setParentId(permission.getParentId());
        }
        return addPermission;
    }

    /**
     * 添加目录级别
     *
     * @param permission
     * @return
     */
    public static Permission addMark(AddPermission permission) {

        Permission addPermission = Permission.builder()
                .parentId(permission.getParentId())
                .category(2)
                .rank(2)
                .title(permission.getTitle())
                .path(permission.getPath())
                .redirect(permission.getRedirect())
                .state(permission.getState())
                .sort(permission.getSort())
                .icon(permission.getIcon()).build();
        return addPermission;
    }


    /**
     * 添加按钮级别
     *
     * @param permission
     * @return
     */
    public static Permission addButton(AddPermission permission) {

        Permission addPermission = Permission.builder()
                .parentId(permission.getParentId())
                .category(3)
                .rank(4)
                .title(permission.getTitle())
                .mark(permission.getMark())
                .state(permission.getState())
                .icon(permission.getIcon()).build();

        return addPermission;
    }

}

